﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Inventory.template
{
    public partial class BasepurchaseBillFrom : Form
    {
        List<int> selectIndexValue = new List<int>() { 0, 1, 2 };
        int ferryValue = 0;         //定义摆渡值：针对不同的查询条件进行不同的操作
        DateTime startD, endD;      //记录查询日期的开始时间、结束时间
        List<string> purchaseIDList;//用于订单编号的列表集合
        string searchStr;           //记录查询字符串
        BasepurchaseBillBLL b1;         //业务逻辑访问类
        //以下是在子类中需要使用到的对象
        protected EmploveeBLL emBLL;              
        protected PurchaseBill bo;               //订单实体类
        protected PurchaseDateil pDetail;//详单实体类
        protected List<PurchaseDateil> detailList;//订单详情泛型集合
        protected DataGridViewTextBoxColumn dgvText;//DataGridView文本框控件
        public BasepurchaseBillFrom()
        {
            InitializeComponent();
            b1 = new BasepurchaseBillBLL();
            ControInittialState();

        }
        private void ControInittialState()
        {
            dtpPurchaseDate .Enabled = false;
            cbSupplier.Enabled = txtOnpress.Enabled = txtMemo.Enabled = lbPurchaseBill.Enabled = txtTotal.Enabled = false;
            comboBox1.Hide();
        }

        public  void cbCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            ControInittialState();
            if (selectIndexValue.Contains(cbCondition.SelectedIndex))
            {
                txtSearchString.Show();//显示查询值文本输入框
                comboBox1.Hide();//影藏执行状态下拉菜单
            }
            if (cbCondition.SelectedIndex==3)
            {
                comboBox1.Show();
                txtSearchString.Hide();
            }
            if (cbCondition.SelectedIndex == 4)
            {
                txtSearchString.Show();
                comboBox1.Hide();
            }


        }
        private void Valuation()
        {
            List<int> tmpList = new List<int> { 0, 1, 2, 3, 4 };
            if (tmpList.Contains(cbCondition.SelectedIndex))
            {
                ferryValue = cbCondition.SelectedIndex;
                startD = dtpEndDate.Value;//获取开始时间
                endD = dtpEndDate.Value;//获取结束时间
                if (txtSearchString .TextLength ==0&&selectIndexValue .Contains (ferryValue ))
                { MessageBox.Show("请输入需要查询的条件！", "系统提示：");txtSearchString.Focus(); }
                searchStr = txtSearchString.Text.Trim();
                switch (ferryValue)
                {
                    
                    case 0:
                      
                        purchaseIDList=b1.FindPurchaseBillIDByPurchaseBillID(searchStr);break;
                    case 1:
                       
                        purchaseIDList=b1.FindPurchaseBillIDByClerk (searchStr); break;
                    case 2:
                       
                        purchaseIDList=b1.FindPurchaseBillIDByExaminer (searchStr); break;
                    case 3:
                      
                        
                            purchaseIDList= b1.FindPurchaseBillIDByOnprocess(comboBox1.SelectedIndex);
                        break;
                    case 4:

                        purchaseIDList= b1.FindPurchaseBillIDByPurchaseDate(startD, endD);
                        break;
                }
            }
            else
            {
                MessageBox.Show("请选择查询条件！", "系统提示：");
            }

                        
                }
            
        

       public  void txtSearchString_TextChanged(object sender, EventArgs e)
        {

        }

        public  void btnSearching_Click(object sender, EventArgs e)
        {
            Valuation();//数据校检
            if (purchaseIDList.Count != 0)
            {
                lbPurchaseBill.DataSource = purchaseIDList;
                ClearControls.ClearControl(gbSearch);
            }
            else
                MessageBox.Show("无此订单！", "系统提示");
            lbPurchaseBill.SelectedIndex = -1;
            lbPurchaseBill.Enabled = true;

        } 
        public  string OnPressState(int value)
        {
            string pState = null;
            switch (value)
            {
                case 0:
                    pState = "执行";
                    if (bo.Custodian != null)
                        pState = "执行（已入库）";
                    else if (bo.Examiner != null)
                        pState = "执行（已审核）";
                    else
                        pState = "执行（未审核）";
                    break;
                case 1:
                    pState = "撤销";
                    break;
                case 2:
                    pState = "完成";
                    break;
            }
            return pState;
        }
        public  void lbPurchaseBill_Click(object sender, EventArgs e)
        {
            if (lbPurchaseBill.SelectedItem == null)
                MessageBox.Show("请点击具体订单查询", "系统提示:");
            else
            {
                string purchseID = lbPurchaseBill.SelectedItem.ToString();//获取ListBox中被选定的订单
                //根据订单号获取所有信息
                bo = b1.FindDateilPurchaseBillByPurchaseID(purchseID);
                if (bo == null)
                    MessageBox.Show("未找到该订单的关联数据", "系统提示");
                else
                {
                    dtpPurchaseDate.Value = bo.PurchaseDate;
                    txtPurchaseID.Text = bo.PurchaseBillID;
                    cbSupplier.SelectedValue = bo.SupplierID;
                    txtMemo.Text = bo.Memo;
                    if (bo.StockDate == null)
                        txtStockDate.Text = "等待入库";
                    else
                        txtStockDate.Text =bo.StockDate.ToString();
                    txtOnpress.Text = OnPressState(bo.OnProcess);
                    //switch (bo.OnProcess)
                    //{
                    //    case 0:
                           
                    //        break;
                    //    case 1:
                    //        txtOnpress.Text = OnPressState(bo.OnProcess);
                    //        break;
                    //    case 2:
                    //        txtOnpress.Text = OnPressState(bo.OnProcess);
                    //        break;

                    //}

                }
                detailList = bo.PurchaseDetailList;
                if (detailList.Count == 0)
                    dgvPurchaseDetail.DataSource = null;
                else
                    BindData();

            } 
        }/// <summary>
        /// 数据源绑定
        /// </summary>
        public  void BindData()
        {
            dgvPurchaseDetail.DataSource = detailList;
            dgvPurchaseDetail.Columns[0].DataPropertyName = "PurchaseID";
            dgvPurchaseDetail.Columns[0].HeaderText = "订单编号";
            dgvPurchaseDetail.Columns[0].Width = 100;
            dgvPurchaseDetail.Columns[0].ReadOnly = true;

            dgvPurchaseDetail.Columns[1].DataPropertyName = "PurchaseDetailID";
            dgvPurchaseDetail.Columns[1].HeaderText = "详单编号";
            dgvPurchaseDetail.Columns[1].Width = 100;
            dgvPurchaseDetail.Columns[1].ReadOnly = true;

            dgvPurchaseDetail.Columns[2].DataPropertyName = "ProductID";
            dgvPurchaseDetail.Columns[2].HeaderText = "chanping";
            dgvPurchaseDetail.Columns[2].Width = 100;
            dgvPurchaseDetail.Columns[2].ReadOnly = true;

            dgvPurchaseDetail.Columns[3].DataPropertyName = "PurchasePrice";
            dgvPurchaseDetail.Columns[3].HeaderText = "购入价格";
            dgvPurchaseDetail.Columns[3].Width = 100;
            dgvPurchaseDetail.Columns[3].ReadOnly = false ;

            dgvPurchaseDetail.Columns[4].DataPropertyName = "Quantity";
            dgvPurchaseDetail.Columns[4].HeaderText = "数量";
            dgvPurchaseDetail.Columns[4].Width = 100;
            dgvPurchaseDetail.Columns[4].ReadOnly = false;

            dgvPurchaseDetail.Columns[5].DataPropertyName = "Account";
            dgvPurchaseDetail.Columns[5].HeaderText = "金额";
            dgvPurchaseDetail.Columns[5].Width = 100;
            dgvPurchaseDetail.Columns[5].ReadOnly = false;
            for (int i=0; i<detailList.Count; i++)
            {
                var price = Convert .ToDecimal ( dgvPurchaseDetail.Rows[i].Cells[3].Value.ToString ());//获取对应行购入价格
                var number= Convert .ToInt32 ( dgvPurchaseDetail.Rows[i].Cells[4].Value.ToString ());//获取对应行购入数量
                dgvPurchaseDetail.Rows[i].Cells[5].Value = price * number;//计算对应行产品金额
            }
        }
        public  void dgvPurchaseDetail_Click(object sender, EventArgs e)
        {
            
        }
        public  void dgvPurchaseDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvPurchaseDetail.CurrentRow.Selected = true;
            pDetail = detailList[e.RowIndex];
            dgvPurchaseDetail.BeginEdit(true);
        }
        public  void dgvPurchaseDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < detailList.Count; i++)
            {
                var price = Convert.ToDecimal(dgvPurchaseDetail.Rows[i].Cells[3].Value.ToString());//获取对应行购入价格
                var number = Convert.ToInt32(dgvPurchaseDetail.Rows[i].Cells[4].Value.ToString());//获取对应行购入数量
                dgvPurchaseDetail.Rows[i].Cells[5].Value = price * number;//计算对应行产品金额
            }

        }

        public  void dgvPurchaseDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2)
                e.Value = new ProductBLL().FindByID(e.Value.ToString()).ProductName;
        }

        public  void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
